﻿using System;
using System.Collections.Generic;
using Mbs.ComponentModel.Container;
using Mbs.Messaging;

namespace Mbs
{
    /// <summary>
    /// MVP 模式中的Presenter类
    /// </summary>
    [Serializable]
    public class Presenter : MarshalByRefObject, IPresenter
    {
        /// <summary>
        /// 可回收资源列表
        /// </summary>
        public ICompositeDisposableObject Disposes { get; private set; }

        /// <summary>
        /// 消息总线
        /// </summary>
        [Inject]
        public IMessageBus MessageBus { get; set; }
        /// <summary>
        /// 服务定位器
        /// </summary>
        [Inject]
        public IServiceLocator Locator { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public Presenter()
        {
            Disposes = new CompositeDisposableObject();
        }


        /// <summary>
        /// 初始化Presenter
        /// </summary>
        public void Init()
        {
            var it = Subscribe();
            while (it.MoveNext())
                Disposes.Add(it.Current);

            OnInit();
        }

        protected virtual void OnInit()
        {
        }

        
        /// <summary>
        /// 订阅消息总线上的消息
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerator<IDisposable> Subscribe()
        {
            yield break;
        }

        private IView view;
        /// <summary>
        /// View
        /// </summary>
        public IView View
        {
            get { return view; }
            private set
            {
                AttachView(value);
            }
        }

        /// <summary>
        /// 附加视图
        /// </summary>
        /// <param name="view"></param>
        protected virtual void AttachView(IView view)
        {
            this.view = view;
            Disposes.Add(view);

            var it = view.Attach();
            while (it.MoveNext())
                Disposes.Add(it.Current);

            it = view.Subscribe();
            while (it.MoveNext())
                Disposes.Add(it.Current);

            //view.Init();
            if (view.IsSupportGlobalization)
                view.InitializeResource();
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Disposes.Dispose();
        }

        /// <summary>
        /// 初始化生命周期服务
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            return null;
        }
    }

    /// <summary>
    /// MVP 模式中的泛型Presenter类
    /// </summary>
    public class Presenter<TView> : Presenter, IPresenter<TView> where TView : IView
    {
        /// <summary>
        /// 泛型视图
        /// </summary>
        /// <remarks>懒惰初始化视图</remarks>
        public virtual new TView View
        {
            get
            {
                var view = (TView)base.View;

                if (view != null) 
                    return view;
                view = Locator.Get<TView>();
                AttachView(view);
                return view;
            }
        }

    }
}